// 病床添加
import { useState, useEffect } from "react";
import { Input, Form, Radio, Select, Button, message, Space } from "antd";
import { Medicaldepartment, Bedaddition } from "../../../request/zwyApi";
import { useNavigate } from "react-router-dom"; // 引入useNavigate
import moduleCss from "../../../styles/AddBed.module.css";

function AddBed() {
  const [departments, setDepartments] = useState([]); // 存储科室数据
  const [nurses, setNurses] = useState([]); // 存储护士数据
  const navigate = useNavigate(); // 创建导航实例

  // 使用useEffect钩子来获取科室和护士数据
  useEffect(() => {
    const fetchMedicalData = async () => {
      try {
        const res = await Medicaldepartment({ page: 1, size: 100 });
        setDepartments(
          res.data.map((item) => ({ value: item.name, label: item.name }))
        );
        //res.data中获取护士的数据，而不是科室的数据
        setNurses(
          res.data.map((item) => ({
            value: item.maindoctor,
            label: item.maindoctor,
          }))
        );
      } catch (error) {
        console.error("Error fetching medical data:", error);
      }
    };
    fetchMedicalData();
  }, []);

  // 处理表单提交
  const handleFinish = async (values) => {
    try {
      // 调用Bedaddition接口并传入表单数据
      const response = await Bedaddition(values);
      if (response.code === 1) {
        message.success("床位创建成功！");
      } else {
        message.error("床位创建失败，请重试。");
      }
    } catch (error) {
      console.error("Error during bed addition:", error);
      message.error("发生错误，请稍后再试。");
    }
  };

  // 返回上一页
  const goBack = () => {
    navigate(-1);
  };

  return (
    <div className={moduleCss.box}>
      <Form
        labelCol={{ span: 4 }}
        wrapperCol={{ span: 14 }}
        layout="horizontal"
        style={{ maxWidth: 600 }}
        onFinish={handleFinish} // 使用handleFinish作为onFinish事件的处理函数
      >
        <Form.Item label="床位编号" name="bednumber">
          <Input />
        </Form.Item>
        <Form.Item label="所属科室" name="department">
          <Select style={{ width: "200px" }}>
            {departments.map((dept) => (
              <Select.Option key={dept.value} value={dept.value}>
                {dept.label}
              </Select.Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item label="氧气罐" name="oxygen">
          <Radio.Group>
            <Radio value={1}> 是 </Radio>
            <Radio value={0}> 否 </Radio>
          </Radio.Group>
        </Form.Item>
        <Form.Item label="心电机" name="heart">
          <Radio.Group>
            <Radio value={1}>是</Radio>
            <Radio value={0}>否</Radio>
          </Radio.Group>
        </Form.Item>
        <Form.Item label="呼吸机" name="breath">
          <Radio.Group>
            <Radio value={1}>是</Radio>
            <Radio value={0}>否</Radio>
          </Radio.Group>
        </Form.Item>
        <Form.Item label="急救设施" name="icu">
          <Radio.Group>
            <Radio value={1}>是</Radio>
            <Radio value={0}>否</Radio>
          </Radio.Group>
        </Form.Item>
        <Form.Item label="空调地暖" name="aircondition">
          <Radio.Group>
            <Radio value={1}>是</Radio>
            <Radio value={0}>否</Radio>
          </Radio.Group>
        </Form.Item>
        <Form.Item label="电视外设" name="tv">
          <Radio.Group>
            <Radio value={1}>是</Radio>
            <Radio value={0}>否</Radio>
          </Radio.Group>
        </Form.Item>
        <Form.Item label="责任护士" name="nurse">
          <Select style={{ width: "200px" }}>
            {nurses.map((nurse) => (
              <Select.Option key={nurse.value} value={nurse.value}>
                {nurse.label}
              </Select.Option>
            ))}
          </Select>
        </Form.Item>

        <Space className={moduleCss.butbox}>
          <Button
            type="primary"
            htmlType="submit"
            style={{ marginLeft: "30px" }}
          >
            确认创建
          </Button>
          <Button
            onClick={goBack}
            style={{
              backgroundColor: "#728CB2",
              borderColor: "#728CB2",
              color: "#fff",
              marginLeft: "20px",
            }}
          >
            返回上一级
          </Button>
        </Space>
      </Form>
    </div>
  );
}

export default AddBed;
